package io.github.tangwan.jdk8.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author tangwan
 * @version V1.0
 * created on 2021/10/20 15:19
 * @since JDK 1.8
 */
@MyInterface(name = "唐婉")
public class MyInterfaceTest {

    @MyInterface(name = "张三")
    private String name;

    @MyInterface(name = "李四")
    public void test1(){
        System.out.println("test1()");
    }

    public static void main(String[] args) throws Exception {
        //获取类上注解
        boolean annotationPresent = MyInterfaceTest.class.isAnnotationPresent(MyInterface.class);
        if (annotationPresent){
            MyInterface annotation = MyInterfaceTest.class.getAnnotation(MyInterface.class);
            System.out.println(annotation.name());
        }
        //获取属性上注解
        Field name = MyInterfaceTest.class.getDeclaredField("name");
        if (null != name && name.isAnnotationPresent(MyInterface.class)){
            System.out.println(name.getAnnotation(MyInterface.class).name());
        }
        //获取方法上注解
        Method test1 = MyInterfaceTest.class.getDeclaredMethod("test1");
        if (null != test1 && test1.isAnnotationPresent(MyInterface.class)){
            System.out.println(test1.getAnnotation(MyInterface.class).name());
        }

    }

}
